package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.Authorityinfos;
import com.grad.why.studentsystem.domain.Menu;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.current.CurrentAuthorityInfo;
import com.grad.why.studentsystem.service.AuthorityService;
import com.grad.why.studentsystem.service.MenuService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/studentManageSystem")
public class AuthorityController {
    @Autowired
    private AuthorityService authorityService;

    @Autowired
    private MenuService menuService;

    @RequestMapping("/getAuthorityinfos")
    @ResponseBody
    public Object getAuthorityinfos(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                                    @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                                    @RequestParam(required = false, defaultValue = "", value = "search") String search
    ) {
        try {
            List<CurrentAuthorityInfo> rows = authorityService.getAuthorityInfos(offset, limit,search);
            long total = authorityService.getAuthorityInfosLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/deleteAuthorityInfoById/{ids}")
    @ResponseBody
    public String deleteAuthorityInfoById(HttpServletRequest request, @PathVariable("ids") String ids){
        try{
            HttpSession session = request.getSession();
            int type = (int)session.getAttribute("type");
            String[] idArrays = ids.split("-");
            if(type == 1){
                for (String id : idArrays) {
                    Authorityinfos authorityinfos = authorityService.getAuthorityInfoById(Long.parseLong(id));
                    Menu menu = menuService.getMenuInfoByMenuId(authorityinfos.getMenuid());
                    if(menu.getMenuname().equals("权限管理")){
                        return "管理员不能够删除权限管理模块";
                    }
                }
            }
            for (String id : idArrays) {
                int n = authorityService.delAuthorityInfoById(Long.parseLong(id));
                if (n <= 0) {
                    return null;
                }
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/addAuthorityInfo")
    @ResponseBody
    public String addAuthorityInfo(HttpServletRequest request,Authorityinfos authorityinfos){
        try{
            HttpSession session = request.getSession();
            int checkExist = authorityService.checkAuthorityExist(authorityinfos);
            if(checkExist > 0){
                return "该权限信息重复，请勿重复添加!";
            }else{
                if(authorityinfos.getUsertype()!=1 && menuService.getMenuInfoByMenuId(authorityinfos.getMenuid()).getMenuname().equals("权限管理")){
                    return "非管理员角色不能追加权限管理权限";
                }else{
                    long maxId = authorityService.getMaxId() + 1;
                    authorityinfos.setAuthorityid(maxId);
                    int n = authorityService.insertAuthorityInfoByEtity(authorityinfos);
                    if(n > 0){
                        return "OK";
                    }
                }
            }
            return null;
        }catch (Exception E){
            E.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/updateAuthorityInfo")
    @ResponseBody
    public String updateAuthorityInfo(Authorityinfos authorityinfos){
        try{
            if(authorityinfos.getUsertype()!=1){
                Menu menu = menuService.getMenuInfoByMenuId(authorityinfos.getMenuid());
                if(menu.getMenuname().equals("权限管理")){
                    return "非管理人员,不能追加权限管理模块";
                }else{
                    int rows = authorityService.checkAuthorityExist(authorityinfos);
                    if(rows > 0){
                        return "该权限已存在，请勿重复修改";
                    }
                    int n = authorityService.updateAuthorityByEntity(authorityinfos);
                    if(n <= 0){
                        return null;
                    }
                }
            }else{
                int rows = authorityService.checkAuthorityExist(authorityinfos);
                if(rows > 0){
                    return "该权限已存在，请勿重复修改";
                }
                int n = authorityService.updateAuthorityByEntity(authorityinfos);
                if(n <= 0){
                    return null;
                }
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
